package com.g4m.baselib.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * Created on 2020-01-01
 * <p>Copyright 2008-2020 snsndk.com</p>
 *
 *
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class SdkResponse<T> {
    /**
     * 返回结果状态码，客户端根据此状态判断操作是否成功
     */
    private Integer result = 1;

    /**
     * 返回结果描述串,用于显示给用户的友好提示，由产品定义，此消息应支持国际化
     */
    private String msg = "";

    /**
     * 返回结果数据体
     */
    private T data;

    /**
     * 返回结果扩展数据体
     */
    private Object ext;

    /**
     * 接口过期时间，数据需要本地缓存或轮询时使用，单位毫秒，默认0不过期
     */
    private int expireTime = 0;

    /**
     * 设置状态码
     *
     * @param result 状态码
     */
    public void setResult(int result) {
        this.result = result;
    }

    public static SdkResponse buildResponse(int result) {
        return buildResponse(result, null);
    }

    public static SdkResponse buildResponse(int result, String msg) {
        return buildResponse(null, result, msg);
    }

    @SuppressWarnings("unchecked")
    public static SdkResponse buildResponse(Object data, int result, String msg) {
        SdkResponse<Object> response = new SdkResponse();
        if (StringUtils.isNotBlank(msg)) {
            response.setMsg(msg);
        }
        response.setResult(result);
        response.setData(data);
        return response;
    }

    public static SdkResponse buildSuccessResponse() {
        SdkResponse response = new SdkResponse();
        response.setResult(WebConstants.RESPONSE_RESULT_SUCCESS);
        return response;
    }

    @SuppressWarnings("unchecked")
    public static SdkResponse buildSuccessResponse(Object data) {
        SdkResponse<Object> response = new SdkResponse();
        response.setResult(WebConstants.RESPONSE_RESULT_SUCCESS);
        response.setData(data);
        return response;
    }

    @SuppressWarnings("unchecked")
    public static SdkResponse buildSuccessResponse(Object data, String msg) {
        SdkResponse<Object> response = new SdkResponse();
        if (StringUtils.isNotBlank(msg)) {
            response.setMsg(msg);
        }
        response.setResult(WebConstants.RESPONSE_RESULT_SUCCESS);
        response.setData(data);
        return response;
    }
}
